package com.freeLearning.boot.system.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.freeLearning.boot.common.annotation.Log;
import com.freeLearning.boot.common.enums.LogModuleEnum;
import com.freeLearning.boot.common.result.Result;
import com.freeLearning.boot.system.model.entity.CourseCategory;
import com.freeLearning.boot.system.model.vo.CourseCategoryListVO;
import com.freeLearning.boot.system.service.CourseCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "17.课程分类接口")
@RequestMapping("/api/v1/course/category")
@RestController
@RequiredArgsConstructor
public class CourseCategoryController {

    private final CourseCategoryService courseCategoryService;

    @GetMapping("/list")
    @Operation(summary = "查询课程分类")
    @Log(value = "查询课程分类", module = LogModuleEnum.COURSECATEGORY)
    @Cacheable(value = "courseCategory", key = "3",sync = true)
    public Result<List<CourseCategoryListVO>> getCategoryList(){
        List<CourseCategory> list = courseCategoryService.list(new QueryWrapper<CourseCategory>().eq("parent_id",0));
        List<CourseCategoryListVO> VOList = list.stream().map(item -> {
            CourseCategoryListVO courseCategoryListVO = BeanUtil.copyProperties(item, CourseCategoryListVO.class);
            courseCategoryListVO.setChildren(courseCategoryService.list(new QueryWrapper<CourseCategory>().eq("parent_id",item.getId())));
            return courseCategoryListVO;
        }).toList();
        return Result.success(VOList);
    }

    @PostMapping("/add")
    @Operation(summary = "新增课程分类")
    @Log(value = "新增课程分类", module = LogModuleEnum.COURSECATEGORY)
    @CacheEvict(value = "courseCategory",allEntries = true)
    public Result<Boolean> addCourseCategory(@RequestBody CourseCategory courseCategory){
        return Result.success(courseCategoryService.save(courseCategory));
    }
    @PostMapping("/update")
    @Operation(summary = "修改课程分类")
    @Log(value = "修改课程分类", module = LogModuleEnum.COURSECATEGORY)
    @CacheEvict(value = "courseCategory",allEntries = true)
    public Result<Boolean> updateCourseCategory(@RequestBody CourseCategory courseCategory){
        return Result.success(courseCategoryService.updateById(courseCategory));
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除课程分类")
    @Log(value = "删除课程分类", module = LogModuleEnum.COURSECATEGORY)
    @CacheEvict(value = "courseCategory",allEntries = true)
    public Result<Boolean> deleteCourseCategory(@PathVariable Long id){
        if (!courseCategoryService.list(new QueryWrapper<CourseCategory>().eq("parent_id",id)).isEmpty()){
            return Result.failed("请先删除子分类");
        }
        return Result.success(courseCategoryService.removeById(id));
    }
}
